package com.tmpt.utils;

import java.sql.Timestamp;
import java.util.*;

import java.text.ParseException;


public class DateUtils {
	public static String beforeDate(String strFormat) {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				strFormat);
		Calendar cal = DateTool.getCalendarInstance();
		cal.add(Calendar.DATE, -1);

		return format.format(cal.getTime());
	}

	/**
	 * 功能：输出上个指定时间周期指定日期格式的日期数据
	 * 
	 * @param nTimeType
	 *            如：Calendar.DATE、Calendar.MONTH
	 * @param strFormat
	 *            如："yyyy-MM-dd"
	 * @return
	 */
	public static String beforeTime(int nTimeType, String strFormat) {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				strFormat);
		Calendar cal = DateTool.getCalendarInstance();
		cal.add(nTimeType, -1);

		return format.format(cal.getTime());
	}

	/*****
	 * 功能：转换日期类型
	 * 
	 * @param strDate
	 * @param nType
	 *            类型：0--8位日期类型转为10位日期类型，20080808-->2008-08-08 1--10位日期类型转为8位日期类型
	 * @return
	 */
	public static String convert(String strDate, int nType) {
		StringBuffer sbDate = new StringBuffer();

		switch (nType) {
		case 0:
			sbDate.append(strDate.substring(0, 4)).append("-").append(
					strDate.substring(4, 6)).append("-").append(
					strDate.substring(6, 8));
			break;
		}

		return sbDate.toString();
	}

	public static String currentDate(String strFormat) {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				strFormat);
		Calendar cal = DateTool.getCalendarInstance();

		return format.format(cal.getTime());
	}
	
	public static String formatDate(Date date,String strFormat){
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				strFormat);
		return format.format(date.getTime());
	}

	public static String currentDayOfMonthStr() {
		Calendar cal = DateTool.getCalendarInstance();
		int currDay = cal.get(Calendar.DAY_OF_MONTH);
		String strTmp = "0" + String.valueOf(currDay);
		return strTmp.substring(strTmp.length() - 2, strTmp.length());
	}

	public static String currentMonthStr() {
		Calendar cal = DateTool.getCalendarInstance();
		int currMonth = cal.get(Calendar.MONTH) + 1;
		String strTmp = "0" + String.valueOf(currMonth);
		return strTmp.substring(strTmp.length() - 2, strTmp.length());
	}

	/**
	 * 功能：输出当前时间指定日期格式的日期数据
	 * 
	 * @param strFormat
	 *            如："yyyy-MM-dd"
	 * @return
	 */
	public static String currentTime(String strFormat) {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				strFormat);
		Calendar cal = DateTool.getCalendarInstance();

		return format.format(cal.getTime());
	}

	public static String currentWeekDayCNStr() {
		Calendar cal = DateTool.getCalendarInstance();
		int currWeekDay = cal.get(Calendar.DAY_OF_WEEK);
		String sWeekDay = null;
		switch (currWeekDay) {
		case 1:
			sWeekDay = "星期日";
			break;
		case 2:
			sWeekDay = "星期一";
			break;
		case 3:
			sWeekDay = "星期二";
			break;
		case 4:
			sWeekDay = "星期三";
			break;
		case 5:
			sWeekDay = "星期四";
			break;
		case 6:
			sWeekDay = "星期五";
			break;
		case 7:
			sWeekDay = "星期六";
			break;
		}

		return sWeekDay;
	}

	public static String currentYearStr() {
		Calendar cal = DateTool.getCalendarInstance();
		int currYear = cal.get(Calendar.YEAR);
		return String.valueOf(currYear);
	}

	public static void main(String[] args) {
		//.out.println(DateUtils.currentDate("yyyy-MM-01"));
		//.out.println(DateUtils.nextTime(Calendar.MONTH,
		//		"yyyy-MM-01"));
	}

	public static String nextDate(String strFormat) {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				strFormat);
		Calendar cal = DateTool.getCalendarInstance();
		cal.add(Calendar.DATE, 1);

		return format.format(cal.getTime());
	}

	public static String nextDate(String strDate, String strFormat)
			throws ParseException {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				strFormat);
		Calendar cal = DateTool.getCalendarInstance();
		Date date = format.parse(strDate);
		cal.setTime(date);
		cal.add(Calendar.DATE, 1);

		return format.format(cal.getTime());
	}

	/**
	 * 功能：输出下个指定时间周期指定日期格式的日期数据
	 * 
	 * @param nTimeType
	 *            如：Calendar.DATE、Calendar.MONTH
	 * @param strFormat
	 *            如："yyyy-MM-dd"
	 * @return
	 */
	public static String nextTime(int nTimeType, String strFormat) {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				strFormat);
		Calendar cal = DateTool.getCalendarInstance();
		cal.add(nTimeType, 1);

		return format.format(cal.getTime());
	}

	public static String parseDateCN(String strDate) {
		StringTokenizer ken = new StringTokenizer(strDate, "-");
		StringBuffer sb = new StringBuffer();
		if (ken.hasMoreElements()) {
			sb.append((String) ken.nextElement()).append("年");
			if (ken.hasMoreElements()) {
				sb.append((String) ken.nextElement()).append("月");
				if (ken.hasMoreElements()) {
					sb.append((String) ken.nextElement()).append("日");
					strDate = sb.toString();
				}
			}
		}

		return strDate;
	}

	public static String parseDateEN(String strDate) {
		StringTokenizer ken = new StringTokenizer(strDate, "-");
		StringBuffer sb = new StringBuffer();
		if (ken.hasMoreElements()) {
			sb.append((String) ken.nextElement()).append("y");
			if (ken.hasMoreElements()) {
				sb.append((String) ken.nextElement()).append("m");
				if (ken.hasMoreElements()) {
					sb.append((String) ken.nextElement()).append("d");
					strDate = sb.toString();
				}
			}
		}

		return strDate;
	}

	public static String thisDate(Calendar cal, String strFormat) {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				strFormat);

		return format.format(cal.getTime());
	}

	/**   
	 * 得到本月的第一天   
	 * @return   
	 */    
	public static String getMonthFirstDay(String formatStr) {     
	    Calendar calendar = DateTool.getCalendarInstance();     
	    calendar.set(Calendar.DAY_OF_MONTH, calendar     
	            .getActualMinimum(Calendar.DAY_OF_MONTH));     
	    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
	    		formatStr);
	    return format.format(calendar.getTime());     
	}     
	    
	/**   
	 * 得到本月的最后一天   
	 *    
	 * @return   
	 */    
	public static String getMonthLastDay(String formatStr) {     
	    Calendar calendar = DateTool.getCalendarInstance();     
	    calendar.set(Calendar.DAY_OF_MONTH, calendar     
	            .getActualMaximum(Calendar.DAY_OF_MONTH));     
	    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
	    		formatStr);
	    return format.format(calendar.getTime());     
	}


	/**
	 * 将字符串格式的日期转换为timestamp格式的
	 * @param date 日期格式为yyyy-MM-dd 或者 yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Timestamp string2timestamp(String date){
		if(date==null)return null;
		try {
			if(date.trim().length() == 10){
				date += " 00:00:00";
			}
			return Timestamp.valueOf(date);
		}catch (Exception e){
			return null;
		}
	}

	public static String timestamp2String(Timestamp timestamp){
		return DateTool.date2String(timestamp,"yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * DateUtil constructor comment.
	 */
	public DateUtils() {
		super();
	}
}